package com.itheima.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/upload")
    @ResponseBody
    public String uploadFile(HttpServletRequest request, MultipartFile uploadFile) {
//        获取项目的根目录
        try {
//            String rootPath = request.getSession().getServletContext().getRealPath("");
            String picPath = "/images/" + UUID.randomUUID().toString() + uploadFile.getOriginalFilename();
            uploadFile.transferTo(new File("D:\\class164\\" + picPath));
            return "http://images.heimays.com" + picPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
